package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dao.TFlupDao;
import com.ruoyi.system.domain.dto.TFlupInfoDto;
import com.ruoyi.system.domain.po.TFlupInfo;
import com.ruoyi.system.domain.po.TFlupInfoExample;
import com.ruoyi.system.domain.vo.TFlupInfoVo;
import com.ruoyi.system.mapper.TFlupInfoMapper;
import com.ruoyi.system.service.TFlupInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class TFlupInfoServiceImpl implements TFlupInfoService {
    
    @Resource
    private TFlupInfoMapper flupInfoMapper;
    @Resource
    private TFlupDao tFlupDao;
    
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertFlupInfo(TFlupInfoDto.Insert flupInfoInsert) {
        // 新增实体数据
        long flupInfoId = IdUtil.getSnowflake().nextId();
        TFlupInfo flupInfo = BeanUtil.copyProperties(flupInfoInsert, TFlupInfo.class);
        flupInfo.setId(flupInfoId);
        flupInfo.setCreateBy(SecurityUtils.getUsername());
        flupInfo.setCreateTime(new Date());
        return flupInfoMapper.insertSelective(flupInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateFlupInfo(TFlupInfoDto.Update flupInfoUpdate) {
        // 修改实体
        TFlupInfo flupInfo = BeanUtil.copyProperties(flupInfoUpdate, TFlupInfo.class);
        flupInfo.setUpdateBy(SecurityUtils.getUsername());
        flupInfo.setUpdateTime(new Date());
        return flupInfoMapper.updateByPrimaryKeySelective(flupInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteFlupInfo(TFlupInfoDto.Delete flupInfoDelete) {
        TFlupInfoExample flupInfoExample = new TFlupInfoExample();
        flupInfoExample.createCriteria().andIdIn(flupInfoDelete.getIdList());
        TFlupInfo flupInfo = new TFlupInfo();
        flupInfo.setDelFlag((byte) 2);
        return flupInfoMapper.updateByExampleSelective(flupInfo, flupInfoExample);
    }

    @Override
    public PageInfo<TFlupInfoVo.Result> listFlupInfo(TFlupInfoDto.Query queryFlupInfo) {
        TFlupInfoExample flupInfoExample = new TFlupInfoExample();
        TFlupInfoExample.Criteria criteria = flupInfoExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (StrUtil.isNotBlank(queryFlupInfo.getName())) {
            criteria.andNameLike("%" + queryFlupInfo.getName() + "%");
        }
        if (queryFlupInfo.getRiskLevel() != null) {
            if (queryFlupInfo.getRiskLevel() == 0) {
                criteria.andRiskLevelIsNull();
            }else {
                criteria.andRiskLevelEqualTo(queryFlupInfo.getRiskLevel());
            }
        }
        if (StrUtil.isNotEmpty(queryFlupInfo.getIdCard())) {
            criteria.andIdcardEqualTo(queryFlupInfo.getIdCard());
        }
        if (queryFlupInfo.getFileNum() != null) {
            criteria.andFileNumEqualTo(queryFlupInfo.getFileNum());
        }
        if (StrUtil.isNotBlank(queryFlupInfo.getTreatmentType())) {
            criteria.andTreatmentTypeLike("%" + queryFlupInfo.getTreatmentType() + "%");
        }
        if (queryFlupInfo.getRound() != null) {
            criteria.andRoundEqualTo(queryFlupInfo.getRound());
        }
        if (queryFlupInfo.getEnterTimeStart() != null && queryFlupInfo.getEnterTimeEnd() != null) {
            criteria.andEnterTimeBetween(queryFlupInfo.getEnterTimeStart(), queryFlupInfo.getEnterTimeEnd());
        }
        if (queryFlupInfo.getLeaveTimeStart() != null && queryFlupInfo.getLeaveTimeEnd() != null) {
            criteria.andLeaveTimeBetween(queryFlupInfo.getLeaveTimeStart(), queryFlupInfo.getLeaveTimeEnd());
        }
        if (queryFlupInfo.getAuditStatus() != null) {
            criteria.andAuditStatusEqualTo(queryFlupInfo.getAuditStatus());
        }
        if (queryFlupInfo.getDeathFlag() != null) {
            criteria.andDeathFlagEqualTo(queryFlupInfo.getDeathFlag());
        }
        if (queryFlupInfo.getLostFlupFlag() != null) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -100);
            Date date = c.getTime();
            if (queryFlupInfo.getLostFlupFlag() == 0) {
                //超期 住院时间大于97天
                criteria.andEnterTimeLessThan(date);
            }else {
                //未超期住院时间小于97
                criteria.andEnterTimeGreaterThan(date);

            }


        }
        if (queryFlupInfo.getSource() != null) {
            criteria.andSourceEqualTo(queryFlupInfo.getSource());
        }
        flupInfoExample.setOrderByClause("create_time desc");
        List<TFlupInfo> flupInfoList = flupInfoMapper.selectByExample(flupInfoExample);
        PageInfo<TFlupInfo> tFlupInfoPageInfo = new PageInfo<>(flupInfoList);
        List<TFlupInfoVo.Result> collect = flupInfoList.stream().map(e -> BeanUtil.copyProperties(e, TFlupInfoVo.Result.class)).collect(Collectors.toList());
        PageInfo<TFlupInfoVo.Result> resultPageInfo = new PageInfo<>(collect);
        resultPageInfo.setPageSize(tFlupInfoPageInfo.getPageSize());
        resultPageInfo.setPageNum(tFlupInfoPageInfo.getPageNum());
        resultPageInfo.setEndRow(tFlupInfoPageInfo.getEndRow());
        resultPageInfo.setStartRow(tFlupInfoPageInfo.getStartRow());
        resultPageInfo.setTotal(tFlupInfoPageInfo.getTotal());
        return resultPageInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pulling() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //获取当前日期
        LocalDate nowDate = LocalDate.now();
        //设置零点
        LocalDateTime beginTime = LocalDateTime.of(nowDate, LocalTime.MIN);
        //将时间进行格式化
        String time1= beginTime.format(dtf);

        //设置当天的结束时间
        LocalDateTime endTime = LocalDateTime.of(nowDate,LocalTime.MAX);
        //将时间进行格式化
        String time2 =dtf.format(endTime);

        List<TFlupInfoDto.Insert> query = tFlupDao.query(time1, time2);

        if (CollectionUtil.isNotEmpty(query)) {
            for (TFlupInfoDto.Insert insert : query) {
                // 新增实体数据
                long flupInfoId = IdUtil.getSnowflake().nextId();
                TFlupInfo flupInfo = BeanUtil.copyProperties(insert, TFlupInfo.class);
                flupInfo.setId(flupInfoId);
                flupInfo.setTreatmentType(insert.getZllx());
                flupInfoMapper.insertSelective(flupInfo);
            }
        }
    }
}
